package com.xzt.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RequestMapping("/chat")
@RestController
public class ChatClientController {

    @Autowired
    private ChatClient chatClient;

    @GetMapping(value = "/ai", produces = MediaType.TEXT_EVENT_STREAM_VALUE + ";charset=utf-8")
    public Flux<String> generation(String userInput) {
        return this.chatClient.prompt()
                .user(userInput)
                .stream()
                .content();
    }

    record recipe(String dish, List<String> ingredients){}

    @GetMapping("/recipe")
    public String record(String userInput){
        String content = chatClient.prompt()
                .user(String.format("请你告诉我%s的食谱", userInput))
                .call()
                .content();
        return content.toString();
    }

    record RoadMainRole(String RoleName,String introduction){}

    @GetMapping("/RoadMainRole")
    public String RoadMainRole(String userInput){
        RoadMainRole role=chatClient.prompt()
                .user(String.format("介绍一下%s这个角色", userInput))
                .call()
                .entity(RoadMainRole.class);
        return role.toString();
    }

    @GetMapping(value = "/RoadMainRole2",produces = "text/html;charset=utf-8")
    public Flux<String> RoadMainRole2(String userInput){
        return chatClient.prompt()
                .user(String.format("介绍一下%s这个角色", userInput))
                .stream()
                .content();
    }


}
